package com.xyz.gulimall.ware.service.impl;

import com.xyz.gulimall.ware.entity.PurchaseEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyz.common.utils.PageUtils;
import com.xyz.common.utils.Query;

import com.xyz.gulimall.ware.dao.PurchaseDetailDao;
import com.xyz.gulimall.ware.entity.PurchaseDetailEntity;
import com.xyz.gulimall.ware.service.PurchaseDetailService;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

     @Override
     public PageUtils queryPage(Map<String, Object> params) {
         QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<PurchaseDetailEntity>();
         String key = (String)params.get("key");
         String status = (String)params.get("status");
         String wareId = (String)params.get("wareId");
         System.err.println(key+"\n"+status+"\n"+wareId);
         if(!StringUtils.isEmpty(wareId)){
             queryWrapper.eq("ware_id",wareId);
         }
         if(!StringUtils.isEmpty(status)){
             queryWrapper.eq("status",status);
         }
         if(!StringUtils.isEmpty(key)){
             queryWrapper.and((iem)->{
                 iem.eq("id",key).or()
                         .like("sku_num",key).or()
                         .like("purchase_id",key);
             });
         }

         IPage<PurchaseDetailEntity> page = this.page(
                      new Query<PurchaseDetailEntity>().getPage(params),
                      queryWrapper
              );
              return new PageUtils(page);
     }

    @Override
    public List<PurchaseDetailEntity> listDetaIlByPurchaseId(PurchaseEntity item) {
        List<PurchaseDetailEntity> list = this.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", item.getId()));
        return list;
    }

}